import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { FormControl } from '@angular/forms';
import getISOWeek from 'date-fns/getISOWeek';
interface Person {
  // tslint:disable-next-line: ban-types
  key: Number;
  them: string;
  content: string;
  principal: string;
  principalId: number;
  lecturer: string;
  lecturerId: number;
  location: string;
  startingTime: string;
  endingTime: string;
  participants: string;
}
@Component({
  selector: 'app-training-management',
  templateUrl: './training-management.component.html',
  styleUrls: ['./training-management.component.less']
})
export class TrainingManagementComponent implements OnInit {
  info: Person;
  chooseOne: string;
  name = new FormControl('');    // FormControl 表单实例
  listOfData: Person[] = [
    {
      key: 1,
      them: '宏',
      content: '数据类型',
      principal: '张三',
      principalId: 12,
      lecturer: '李四',
      lecturerId: 14,
      location: 'A会议室',
      startingTime: '2020-12-15',
      endingTime: '2020-12-16',
      participants: 'IT部门',
    },
    {
      key: 3,
      them: '宏3',
      content: '数据类型3',
      principal: '张三3',
      principalId: 123,
      lecturer: '李四3',
      lecturerId: 14,
      location: 'A会议室3',
      startingTime: '2020-12-153',
      endingTime: '2020-12-163',
      participants: 'IT部门3',
    },
    {
      key: 2,
      them: '宏2',
      content: '数据类型2',
      principal: '张三2',
      principalId: 122,
      lecturer: '李四2',
      lecturerId: 142,
      location: 'A会议室2',
      startingTime: '2020-12-152',
      endingTime: '2020-12-162',
      participants: 'IT部门2',
    }
  ];
  i: string;
  visible = false;   // 新增抽屉控制
  demoValue = 3;     // 新增 讲师工号
  form: FormGroup;
  constructor(public fb: FormBuilder) { }
  // date = Date;
  // onChange(result: Date[]): void {
  //   console.log('onChange: ', result);
  // }
  open(i, data?: any): void {    // 打开新增抽屉
    this.visible = true;
    if (i === 1) {
      this.chooseOne = '添加';
    }
    if (i === 2) {
      this.chooseOne = '修改';
      this.info = data;
      this.form = this.fb.group({
        them: [this.info.them, Validators.required],
        key: [this.info.key, Validators.required],
        content: [this.info.content, Validators.required],
        principal: [this.info.principal, Validators.required],
        principalId: [this.info.principalId, Validators.required],
        lecturer: [this.info.lecturer, Validators.required],
        lecturerId: [this.info.lecturerId, Validators.required],
        location: [this.info.location, Validators.required],
        startingTime: [this.info.startingTime, Validators.required],
        endingTime: [this.info.endingTime, Validators.required],
        participants: [this.info.participants, Validators.required],
      });
    }
  }

  close(): void {   // 关闭新增抽屉
    this.visible = false;
  }

  ngOnInit(): void {
    this.form = this.fb.group({
      them: ['', Validators.required],
      key: ['', Validators.required],
      content: ['', Validators.required],
      principal: ['', Validators.required],
      principalId: ['', Validators.required],
      lecturer: ['', Validators.required],
      lecturerId: ['', Validators.required],
      location: ['', Validators.required],
      startingTime: ['', Validators.required],
      endingTime: ['', Validators.required],
      participants: ['', Validators.required],
    });
  }
  OnSubmit(): void {
    this.visible = false;
    console.log(this.form.value);
  }

}
